package com.fpd.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * @desc 线程池配置
 * @author wujiangbo
 * @date 2019年11月15日 上午11:22:00
 */
@Configuration
@ConfigurationProperties(prefix = "threadpool")
public class ExecutePoolConfiguration {
	
	@Value("${threadpool.core-pool-size}")
	private int corePoolSize;
	
	@Value("${threadpool.max-pool-size}")
	private int maxPoolSize;
	
	@Value("${threadpool.queue-capacity}")
	private int queueCapacity;
	
	@Value("${threadpool.keep-alive-seconds}")
	private int keepAliveSeconds;

	@Bean(name = "threadPoolTaskExecutor")
	public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
		ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
		pool.setKeepAliveSeconds(keepAliveSeconds);
		pool.setCorePoolSize(corePoolSize);// 核心线程池数
		pool.setMaxPoolSize(maxPoolSize); // 最大线程
		pool.setQueueCapacity(queueCapacity);// 队列容量
		pool.setRejectedExecutionHandler(new java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy()); // 队列满，线程被拒绝执行策略
		return pool;
	}
}
